package cn.yili12.common.util;

import cn.hutool.core.lang.Snowflake;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Scope("singleton")  //原型Prototype    单例Singleton
public class IdTool {

    @Value("${id.workerId:1}")
    private Integer workerId;  //参数1为终端ID

    @Value("${id.datacenterId:1}")
    private Integer datacenterId; //参数2为数据中心ID

    private Snowflake snowflake;

    public IdTool() {

    }

    @PostConstruct
    public void init() {
        snowflake = new Snowflake(workerId, datacenterId);
    }

    public Long createId() {
        long id = snowflake.nextId();
        return id;
    }
}
